package com.webex.ta.hydra.gui.util;

import java.io.File;
import java.util.Arrays;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-6
 * Time: 8:42:21
 */
public class HydraFileFilter extends javax.swing.filechooser.FileFilter implements java.io.FileFilter {

	private final String[] exts;

    private final boolean allowDirs;


	public HydraFileFilter(String[] extensions) {
        this(extensions,true);
	}

    public HydraFileFilter(String[] extensions, boolean allow) {
        exts = extensions;
        allowDirs = allow;
    }


	public boolean accept(File f) {
		return (allowDirs && f.isDirectory()) || accept(f.getName().toLowerCase());
	}

	public boolean accept(String filename) {
		if (exts.length == 0) {
			return true;
		}

        for (String ext : exts) {
            if (filename.endsWith(ext)) {
                return true;
            }
        }

		return false;
	}

	/**
	 * Get a description for this filter.
	 *
	 * @return a description for this filter
	 */
	public String getDescription() {
		return "Hydra " + Arrays.asList(exts).toString();
	}
}
